package indi.free.widgets.custom_recycler_view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {

    public static final String TAG = "EVENT";
    protected final List<T> mData;
    protected final Context mContext;
    protected LayoutInflater mInflater;
    private OnItemClickListener mClickListener;
    private OnItemLongClickListener mLongClickListener;


    protected SparseArray<ArrayList<RecyclerViewHolder>> preCreatedViewHolders = new SparseArray<>();
    protected ArrayList<Integer> alreadyCreatedViewType = new ArrayList<>();


    public BaseRecyclerAdapter(Context ctx, List<T> list) {
        mData = (list != null) ? list : new ArrayList<T>();
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
    }


    public void preCreateViewHolders(ViewGroup parent, int viewType, int count) {
        Log.d(TAG, "preCreateViewHolders: " + viewType);
        ArrayList<RecyclerViewHolder> holders = preCreatedViewHolders.get(viewType);
        if (null == holders) {
            holders = new ArrayList<>();
        }
        for (int i = 0; i < count; i++) {
            RecyclerViewHolder holder = new RecyclerViewHolder(mContext,
                    mInflater.inflate(getItemLayoutId(viewType), parent, false));
            initPreCreatedViewHolder(holder, i);
            holders.add(holder);
            Log.d(TAG, "preCreateViewHolders: " + i);
        }
        preCreatedViewHolders.append(viewType, holders);

    }


    public RecyclerViewHolder getPreCreatedViewHolder(int viewType) {
        final ArrayList<RecyclerViewHolder> holders = preCreatedViewHolders.get(viewType);


        if (holders != null && !holders.isEmpty()) {

            Log.d(TAG, "getPreCreatedViewHolder: holders size = " + holders.size());

            Log.d(TAG, "getPreCreatedViewHolder: viewType = " + viewType);

            final int index = holders.size() - 1;
            final RecyclerViewHolder holder = holders.get(index);
            holders.remove(index);
            return holder;
        }
        return null;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerViewHolder holder = getPreCreatedViewHolder(viewType);

        if (null == holder) {
            holder = new RecyclerViewHolder(mContext,
                    mInflater.inflate(getItemLayoutId(viewType), parent, false));
            alreadyCreatedViewType.add(viewType);
            Log.d(TAG, "onCreateViewHolder: " + viewType);

        }

        if (mClickListener != null) {
            final RecyclerViewHolder finalHolder = holder;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(finalHolder.itemView, finalHolder.getLayoutPosition());
                }
            });
        }
        if (mLongClickListener != null) {
            final RecyclerViewHolder finalHolder1 = holder;
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mLongClickListener.onItemLongClick(finalHolder1.itemView, finalHolder1.getLayoutPosition());
                    return true;
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        if (position != mData.size()) {
            bindData(holder, position, mData.get(position));
        } else {
            bindData(holder, position, null);
        }

    }

    public T getItem(int pos){
        return mData.get(pos);
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void add(int pos, T item) {
        mData.add(pos, item);
        notifyItemInserted(pos);
    }

    public void delete(int pos) {
        mData.remove(pos);
        notifyItemRemoved(pos);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mLongClickListener = listener;
    }

    abstract public int getItemLayoutId(int viewType);

    abstract public void bindData(RecyclerViewHolder holder, int position, T item);

    protected void initPreCreatedViewHolder(RecyclerViewHolder holder, int position) {
    }

    ;

    public interface OnItemClickListener {
        public void onItemClick(View itemView, int pos);
    }

    public interface OnItemLongClickListener {
        public void onItemLongClick(View itemView, int pos);
    }
}
